home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / chip48v1.doc < prev    next >
Text File  |  1995-03-31  |  11KB  |  246 lines

  1. Item: 713 by gson at niksula.hut.fi 
  2. Author: [Andreas Gustafsson] 
  3.   Subj: CHIP48 - A CHIP-8 interpreter for the HP48SX (version 2.20) 
  4.   Date: Mon Sep 10 1990 08:16  
  5.  
  6. This is the first release of CHIP-48, a CHIP-8 interpreter for the HP48SX. 
  7.  
  8. [NB: Version 2.25, called "CHIP", plus DOC file, are on disk.  -jkh-] 
  9.  
  10. For those who don't remember, the CHIP-8 programming language was used 
  11. in a number of home computers based on RCA's CDP1802 processor in the 
  12. late 1970's.  It's a small, interpreted language designed specifically 
  13. for writing simple video games.  It has less than 40 instructions, 
  14. including arithmetic, control flow, graphics, and sound.  The 
  15. instructions are all 16 bits long and are executed by a very compact 
  16. virtual machine interpreter (the 1802 implementation was 512 bytes long). 
  17. A simple Pong-style game can usually be written in about 256 bytes of 
  18. CHIP-8 and would usually be written directly in hexadecimal. 
  19.  
  20. This posting includes documentation on CHIP-8 (terse but reasonably 
  21. complete), CHIP-48 itself, and a sample game.  Write more and post 
  22. them to comp.sys.handhelds! 
  23.  
  24. Make sure you have your calculator backed up before you try running 
  25. CHIP-48.  The interpreter was written for a HP48SX with the Revision A 
  26. ROM.  It appears that it does _not_ work with other ROM revisions. 
  27. Hopefully users with newer ROMs will make the necessary changes and 
  28. mail them to me. 
  29.  
  30.  
  31. * Technical specification 
  32.  
  33. The CHIP-8 virtual machine is byte-addressable and has an address 
  34. space of 4 kB at addresses 000-FFF hex.  However, addresses 000-1FF 
  35. are reserved (this is where the CHIP-8 interpreter used to reside). 
  36. Therefore, the CHIP-8 program itself begins at address 200.  All  
  37. instructions are 16 bits long and by convention instructions always 
  38. start at an even address. 
  39.  
  40. The machine has 16 8-bit general-purpose registers called V0..VF.  The 
  41. VF register is modified by certain instructions and works as a carry 
  42. flag and sprite collision indicator.  There is also a 16-bit pointer 
  43. register I (though only the low 12 bits are generally used). 
  44.  
  45. A CHIP-8 program displays graphics by drawing sprites that are 8 
  46. pixels wide and 1..15 pixels high.  The screen resolution is 32 by 64 
  47. pixels.  The origin is the upper left corner of the screen, and all 
  48. coordinates are positive.  The sprites are stored in big-endian 
  49. format, i.e., the most significant bit corresponds to the leftmost 
  50. pixel.  All drawing is done in XOR mode.  If this causes one or 
  51. more pixels to be erased, VF is set to 01, otherwise 00. 
  52.  
  53. There are two timers: the delay timer and the sound timer.  Both 
  54. timers count down about 60 times per second when nonzero; the speaker 
  55. will beep whenever the sound timer is nonzero. 
  56.  
  57. In the instruction table below, NNN is a 12-bit address, KK is an 
  58. 8-bit constant, and X and Y are 4-bit register numbers.  Hex 
  59. characters represent themselves.  The two first characters of the 
  60. instruction code form the lower-address byte of the instruction, the 
  61. first character being the more significant nibble. 
  62.  
  63. 0NNN     Call 1802 machine code subroutine at NNN 
  64. 00E0     Clear display 
  65. 00EE     Return from subroutine 
  66. 1NNN     Jump to NNN 
  67. 2NNN     Call subroutine at NNN 
  68. 3XKK     Skip next instruction if VX == KK 
  69. 4XKK     Skip next instruction if VX != KK 
  70. 5XY0     Skip next instruction if VX == VY 
  71. 6XKK     VX := KK 
  72. 7XKK     VX := VX + KK 
  73. 8XY0     VX := VY, VF may change 
  74. 8XY1     VX := VX or VY, VF may change 
  75. 8XY2     VX := VX and VY, VF may change 
  76. 8XY3     VX := VX xor VY, VF may change * 
  77. 8XY4     VX := VX + VY, VF := carry 
  78. 8XY5     VX := VX - VY, VF := not borrow 
  79. 8XY6     VX := VX shr 1, VF := carry 
  80. 8XY7     VX := VY - VX, VF := not borrow * 
  81. 8XYE     VX := VX shl 1, VF := carry 
  82. 9XY0     Skip next instruction if VX != VY 
  83. ANNN     I := NNN 
  84. BNNN     Jump to NNN+V0 
  85. CXKK     VX := pseudorandom_number and KK 
  86. DXYN     Show N-byte sprite from M(I) at coords (VX,VY), VF := collision 
  87. EX9E     Skip next instruction if key VX pressed 
  88. EXA1     Skip next instruction if key VX not pressed 
  89. FX07     VX := delay_timer 
  90. FX0A     wait for keypress, store hex value of key in VX 
  91. FX15     delay_timer := VX 
  92. FX18     sound_timer := VX 
  93. FX1E     I := I + VX 
  94. FX29     Point I to 5-byte font sprite for hex character VX 
  95. FX33     Store BCD representation of VX in M(I)..M(I+2) 
  96. FX55     Store V0..VX in memory starting at M(I) 
  97. FX65     Read V0..VX from memory starting at M(I) 
  98.  
  99. The instructions marked with * may have been undocumented (but 
  100. functional) in the original CHIP-8 interpreter for the 1802. 
  101.  
  102.  
  103. * Notes on the HP48SX implementation  
  104.  
  105. CHIP-8 programs are stored in the HP48SX as string objects containing 
  106. the bytes of the program in increasing address order, beginning with 
  107. the byte at 0200.  The interpreter itself is a machine code object 
  108. that should be run with the CHIP-8 program string on level 1.  4 kB of 
  109. free memory is needed.  If an error is detected during execution, the 
  110. address of the current CHIP-8 instruction is returned as a binary 
  111. integer on level 1. 
  112.  
  113. The clock display should be turned off.  Otherwise, CHIP48 will not run 
  114. but will clear flag -40 so that the next attempt to run it may succeed. 
  115.  
  116. To quit, press the backspace key.  Pressing ENTER restarts the CHIP-8 
  117. program, and the +/- key turns the sound off or on. 
  118.  
  119. The 0NNN instruction is unimplemented (of course), except for  
  120. NNN==0E0 or 0EE.  Subroutine nesting is limited to 16 levels. 
  121.  
  122. Most chip-8 programs are written for a 16-key hex keyboard with  
  123. following layout: 
  124.  
  125.   1 2 3 C                                               7 8 9 / 
  126.   4 5 6 D    This keyboard is emulated on the HP48SX    4 5 6 * 
  127.   7 8 9 E    using the following keys:                  1 2 3 - 
  128.   A 0 B F                                               0 . _ + 
  129.  
  130. This may cause some confusion with programs requiring numerical input. 
  131. The keys 2, 4, 6, and 8 are commonly used to represent directions. 
  132. The F key (+ on the HP48SX) is sometimes used as a Fire button. 
  133.  
  134.  
  135. * The interpreter 
  136.  
  137. Below is a HP48SX machine code object that should be uudecoded and 
  138. then downloaded to the calculator using Kermit. 
  139.  
  140. begin 644 chip 
  141. M2%!(4#0X+4',+7#J /B_BH&_>09#S@870U$""&CS@( T40!F$D@Z!  "^'U; 
  142. M$&,8^J#ZH!_ZHC\8^B%#  3 $D-!%U-!,\2B JBF$4=!@X&/- 3  ;AB8#D1 
  143. M1W<4^-)G$$=^^)MG@*$?&D,82T.5#, 24P@H 17U\!('41QAP9S&ZPCX+1CZ 
  144. M@", @J$/B[$T1-0 +#'4$D07,RTC.!%#0=8:9*B&CX!"@ \ _H*A#W&Y4!H( 
  145. M>-2)+WT&01)&",@(^/A7#8"A']N*@0^!H0]SDQ%3+7$0=1 ,;&P82ZQ#/PS  
  146. M$D,M43X82ZP86QCXLF 8*V1@>1^Q 8A_4P;XFV=@;X^*%E$/40QA$ ?,IOX0 
  147. MAQ5!GJ8K81%ED* ?81#E@A"[CH B  P^$L72,H(3,11DK4&&:O@(* 3X . O 
  148. M&/H1J&9@?] R >@Z!/@ X&\8^A#'#$&>I@AJ'L1A$>1IBJ#F07S', (!"($/ 
  149. M%@&M+0@1Y1.%\PS^AH!'  AX (" -P<# 7-[$&7PCH " ?X216#BWR(2=2 0 
  150. M%T$?'!!L&/HA+! M40=L&$NL0^L*P!)#$"T#8.3?,O"V_2T3$!;]+1,1=OPM 
  151. M$Q+6^RT3%#;[9_HM00X6)X?Y#4$*%RP0<7*%'RPM41Y@9_<-41IP[1!O;P@H 
  152. M$0!N G9Z#Q?$,&P82ZQ#WPG $D,M40X(@0\6 :TM"!$&40[@+Q Q%D-@9_(8 
  153. M^H%'[S$%UPIQTM[:,@&.*@T3[JA"!(*Q-&1\ *P30%TQ$&0QI"]1=&$?11?V 
  154. M08R!#Z36:7T-@[$TI)L +#$4)*@(@(&/$P08^$(L,=02Y8.A#PMS/(ZA#PN# 
  155. ML31$EP L,10D&/A"+#&&H1\3A1-C08*!#S,$! "X!A $!ONGX'$1M$&>9A08 
  156. M^K(8^!(8^K(P1]YQ$;1!GB;NEOU'XL;\Q^&F_N?;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6BF&PL,13DZK<_^_/5HIAL+#$4Y.JW 
  173. M/_OSU:*8;"PQ%.3JAZ$?$0-!)IS@"!CX0!CX0 (8^A 8^B$L,30!LB8(*(&0 
  174. M;@7JIN8(#5$6F@H>EZ$/"-_B X'PK?G@H9L/'H?P+9S@&PC?411A'_9A$45A 
  175. M"X#P'44AH.1):F"G#@$ # //P,S#/S \,__P_///EW">"0><<.()1Z!P)@J' 
  176. MI'!J"L>H<*X*!ZUP\@I'L7 V"X>U<'H+Q[EPO@L'OG "#$?"<$8,A\9PB@S' 
  177. MRG#.# ?/<!(-1]-P5@V'UW":#<?;<-X-!^!P(@Y'Y'!F#H?H<*H.Q^QP[@X' 
  178. M\7 R#T?U<'8/A_EPN@_'_7#^#P<"<4(01P9QAA"'"G'*$(>%<'H(QXEPO@@' 
  179. MCG ""4>2<$8)]YGY8B+W\?@?'Y_Y$8\?__CY'T+T^?F?'__YF9Z>_HCXGIG^ 
  180. M^/B/CQ@  D  "& !,D &*!&$B(1"2$0B*"02&()!(1&1)_ PL#$@-H XX#G@ 
  181. M.M [(#U@.F!+($R034!04%E08@                       !  !        
  182. I                                                         
  183.   
  184. end 
  185.  
  186.  
  187. * Sample game 
  188.  
  189. Not particularly exciting, but I had to include something to show how 
  190. the interpreter is used.  Uuencode, download to HP48SX using Kermit, 
  191. and run with BRIX CHIP.  Play by pressing the keys 4 and 6. 
  192.  
  193. begin 644 brix 
  194. M2%!(4#0X+4$L*E C &X%90!K!FH HPS:L7H$.D 2"'L".Q(2!FP@;1^C$-S1 
  195. M(O9@ &$ HQ+0$7 (HP[0$6! \!7P!S  $C3&#V<>: %I_Z,.UG&C$-S18 3@ 
  196. MH7S^8 ;@H7P"8#^, MS1HP[6<8:$AY1@/X8"81^'$D<?$JQ& &@!1C]H_T<  
  197. M:0'6<3\!$JI''Q*J8 6 =3\ $JI@ ? 8@&!A_( 2HPS0<6#^B0,B]G4!(O9% 
  198. M8!+>$D9I_X!@@,4_ 1+*80* %3\!$N" %3\!$NZ %3\!$NA@(/ 8HPY^_X#@ 
  199. M@ 1A - 1/@ 2,!+>>/](_FC_$NYX 4@": %@!/ 8:?\2<*,4]3/R9?$I8S=D 
  200. 7 --%<P7R*=-% .[@ (  _ "J       " 
  201.   
  202. end 
  203.  
  204.  
  205. * Source code availability 
  206.  
  207. The source code for the interpreter is over 30 k of assembler, and to 
  208. assemble it you need my ASAP assembler which is about 25 k.  To run 
  209. the assembler, you need Perl 3.0 which is several hundred k. 
  210. Therefore, I am not posting all these to comp.sys.handhelds.  However, 
  211. the source to CHIP48 and ASAP are available by anonymous FTP from 
  212. vega.hut.fi, directory /pub/misc/hp48sx/asap.  The source to Perl 3.0 
  213. is available from numerous Unix archive sites. 
  214.  
  215. Those who want to write their own machine code games may want to look 
  216. at the interpreter source for hints.  Please note that I have no 
  217. insider technical information on the HP48SX; CHIP-8 is based solely on 
  218. information posted to comp.sys.handhelds (in particular, Alonzo 
  219. Gariepy's excellent HP28S Processor Notes) and personal HP48SX machine 
  220. code hacking. 
  221.  
  222.  
  223. * Fine print 
  224.  
  225.       CHIP-48 and BRIX are (C) Copyright 1990 Andreas Gustafsson 
  226.  
  227.       Noncommercial distribution allowed, provided that this 
  228.       copyright message is preserved, and any modified versions 
  229.       are clearly marked as such.   
  230.  
  231.       CHIP-48 and BRIX make use of undocumented low-level features of 
  232.       the HP48SX calculator, and may or may not cause loss of data, 
  233.       excessive battery drainage, and/or damage to the calculator 
  234.       hardware.  The Author takes no responsibility whatsoever for  
  235.       any damage caused by the use of this program. 
  236.  
  237.       THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR 
  238.       IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  239.       WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  
  240.       PURPOSE. 
  241.  
  242. --  
  243. Andreas Gustafsson 
  244. Internet: gson@niksula.hut.fi 
  245. Voice: +358 0 563 5592 
  246.